package file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author wang hao
 * @date 2023-04-02 08:56
 */
public class Demo06 {
    public static void main(String[] args) throws IOException {
        File file = new File("src/main/java/file/1.txt");
        System.out.println(file.length());
        FileInputStream fi = new FileInputStream(file);

//        int read = fi.read();
//        System.out.println((char) read);

        int b;
        while ((b = fi.read()) != -1){
            System.out.printf(String.valueOf((char) b));
        }
        System.out.println();

        fi = new FileInputStream(file);
        // 每次按照字节数组读取
        byte[] buffer = new byte[1024];
        int len = fi.read(buffer);
        String s = new String(buffer, 0, len);
        System.out.println(s);

        // 改进 循环每次读取一个字节数组
        buffer = new byte[3];
        fi = new FileInputStream(file);
        len = 0;
        while ((len = fi.read(buffer)) != -1){
            s = new String(buffer, 0, len);
            System.out.printf(s);
        }

    }
}
